/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sn.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import sn.dao.PersonneDAO;
import sn.metier.Personne;

/**
 *
 * @author Harry
 */
@Component
public class ConnectionServiceImpl implements ConnectionService{
    
    @Autowired
    private PersonneDAO personneDAO;

    @Transactional
    @Override
    public Personne sinscrire(String nom, String prenom, String pseudo, String email, String passwd) {
        if(personneDAO.isEmailExistant(email)){
            throw new IllegalArgumentException("L'email "+email+" est déjà utilisée.");
        }
        Personne p = new Personne(nom, prenom, pseudo, email, passwd);
        return personneDAO.save(p);
    }

    @Override
    public Personne seConnecter(String email, String passwd) {
        return personneDAO.findByEmailPasswd(email, passwd);
    }

    @Transactional
    @Override
    public void desincrire(Long id) {
        Personne p = personneDAO.findById(id);
        personneDAO.delete(p);
    }

}
